import java.util.Random;

public class BlackJack
{

	private Card[] deckOfCards;
	
	private static final int NUMBER_OF_CARDS = 52;
	
	private int currentCard = 0;
	
	private static final Random randomNumbers = new Random();
	
	private Card[] dealerHand;
	
	//private Card dealerShownCard;
	
	private Card dealerHoleCard;
	
	private Card[] playerHand;
	
	public BlackJack()
	{
		String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
								"Jack", "Queen", "King"};
		String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
		
		deckOfCards = new Card[NUMBER_OF_CARDS];
		
// 		for(int count = 0; count < deck.lenth; count++)
// 		{
// 			
// 		}

		int cardIndex = 0;
		
		for(int suitCount = 0; suitCount < 4; suitCount++)
		{
			for(int faceCount = 0; faceCount < 13; faceCount++)
			{
				int cardRank = faceCount + 1;
				
				if(cardRank > 10)
					cardRank = 10;
					
				if(cardRank == 1)
					cardRank = 11;
				
				deckOfCards[cardIndex] = new Card(faces[faceCount], suits[suitCount], cardRank);
				
				cardIndex++;
			}
		}
	}
	
	public void Shuffle()
	{
		currentCard = 0;
		
		for(int first = 0; first < deckOfCards.length; first++)
		{
			int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
			
			Card temp = deckOfCards[first];
			deckOfCards[first] = deckOfCards[second];
			deckOfCards[second] = temp;
		}
	}
	
// 	public Boolean CanDeal()
// 	{
// 		return currentCard < deckOfCards.length - 10;
// 	}
	
	public void dealFirstHand()
	{
		dealerHand = new Card[1];
		playerHand = new Card[2];	
		
		playerHand[0] = dealSingleCard();
		playerHand[1] = dealSingleCard();
		
		dealerHand[0] = dealSingleCard();
		//dealerShownCard = dealSingleCard();
		dealerHoleCard = dealSingleCard();
			
	}
	
	public Card[] getDealerHand()
	{
		return dealerHand;
	}
	
	public Card[] getPlayerHand()
	{
		return playerHand;
	}
	
// 	public Card getDealerShownCard()
// 	{
// 		return dealerShownCard;
// 	}
	
	public int playerTotal()
	{
		int total = 0;
		
		Card[] playerCards = getPlayerHand();
		
		for(int i = 0; i<playerCards.length; i++)
		{
			total += playerCards[i].Rank();
		}
		
		if(total > 21)
		{
			checkForAce(playerCards);
			
			total = 0;
			
			for(int i = 0; i<playerCards.length; i++)
			{
				total += playerCards[i].Rank();
			}
		
		}
		
		return total;
	}
	
	public int dealerTotal()
	{
		int total = 0;
		
		Card[] dealerCards = getDealerHand();
		
		for(int i = 0; i<dealerCards.length; i++)
		{
			total += dealerCards[i].Rank();
		}
		
		if(total > 21)
		{
			checkForAce(dealerCards);
			
			total = 0;
			
			for(int i = 0; i<dealerCards.length; i++)
			{
				total += dealerCards[i].Rank();
			}
			
		}
		
		return total;
	}
	
	private void checkForAce(Card[] cards)
	{
			for(int i = 0; i<cards.length; i++)
			{
				if(cards[i].Rank() == 11)
				{
					cards[i].resetAce();
					break;
				}
			}
	}
	
	private Boolean hasAce(Card[] cards)
	{
		for(int i = 0; i<cards.length; i++)
		{
			if(cards[i].Rank() == 11)
			{
				return true;
			}
		}
		
		return false;
	}
	
	private Card dealSingleCard()
	{
		if(currentCard < deckOfCards.length)
		{
			return deckOfCards[currentCard++];
		}
		else
		{
			return null;
		}
	}
	
	public void dealPlayerCard()
	{
		Card playerCard = dealSingleCard();
		
		Card[] newPlayerCards = new Card[getPlayerHand().length + 1];
		
		System.arraycopy(getPlayerHand(), 0, newPlayerCards, 0, getPlayerHand().length);
		
		newPlayerCards[getPlayerHand().length] = playerCard;
		
		playerHand = newPlayerCards; 
	}
	
	public void dealerDraws()
	{
		addElementToDealerHand(dealerHoleCard);
		
		Boolean mustHit = dealerTotal() < 17;
		
		if(dealerTotal() == 17 && hasAce(dealerHand))
		{
			mustHit = true;
		}
		
		while(mustHit)
		{
			Card dealerCard = dealSingleCard();

			addElementToDealerHand(dealerCard);
			
			mustHit = dealerTotal() < 17;
		}
		
		// Card dealerCard = dealSingleCard();
// 		
// 		Card[] newDealerCards = new Card[getDealerHand().length + 1];
// 		
// 		newDealerCards[newDealerCards.length-1] = dealerHoleCard;
// 		
// 		dealerHand = newDealerCards; 	
// 		
// 		
// 		
// 		newDealerCards = new Card[getDealerHand().length + 1];
// 
// 		System.arraycopy(getDealerHand(), 0, newDealerCards, 0, getDealerHand().length);
// 		
// 		newDealerCards[getDealerHand().length] = dealerCard;
// 		
// 		dealerHand = newDealerCards; 	
	}
	
	private void addElementToDealerHand(Card card)
	{
		Card[] newDealerCards = new Card[getDealerHand().length + 1];
		
		System.arraycopy(getDealerHand(), 0, newDealerCards, 0, getDealerHand().length);
		
		newDealerCards[getDealerHand().length] = card;
		
		dealerHand = newDealerCards; 
	}

}